package com.ruoyi.common.tools.excel.listener.asset;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.asset.park.domain.vo.ParkMaintenanceVo;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.region.domain.vo.RegionVo;
import com.ruoyi.erecord.region.service.IRegionService;

public class ParkMaintenanceExcelListener extends MyExcelListener<ParkMaintenanceVo> {

	private DictService dictService = SpringUtils.getBean(DictService.class);

	private IRegionService regionService = SpringUtils.getBean(IRegionService.class);

	private String batchNo;

	private final String cityName = "深圳市";
	private final String DICT_NAME_MAINTENANCE_TYPE = "assetmanage_park_type";

	public ParkMaintenanceExcelListener(String batchNo) {
		this.batchNo = batchNo;
	}

	@Override
	public void doValidate(ParkMaintenanceVo data, AnalysisContext context) {
		String regionId = null;
		String streetId = null;
		String communityId = null;

		List<String> validateList = new ArrayList<>();

		if (StringUtils.isBlank(data.getParkName())) {
			validateList.add("公园名称不能为空");
		}

		if (data.getArea() == null) {
			validateList.add("公园面积不能为空");
		} else if (data.getArea() < 0) {
			validateList.add("公园面积不能为负数");
		}

		if (StringUtils.isNoneBlank(data.getRegion())) {
			// 获取区id
			regionId = regionService.getRegionIdByName(data.getRegion());
			RegionVo regionVo = regionService.queryById(regionId);

			if (regionVo != null && cityName.equals(regionVo.getParentTitle())) {
				data.setRegion(regionId);
			} else {
				validateList.add("区信息编辑不正确");
			}
		} else {
			validateList.add("区不能为空");
		}

		if (StringUtils.isNotBlank(regionId) && StringUtils.isNotBlank(data.getStreet())) {
			// 获街道id
			streetId = regionService.getRegionIdByName(data.getStreet());
			RegionVo regionVo = regionService.queryById(streetId);

			if (regionVo != null && regionId.equals(regionVo.getParentId())) {
				data.setStreet(streetId);
			} else {
				validateList.add("街道信息编辑不正确");
			}
		} else if (StringUtils.isBlank(data.getStreet())) {
			validateList.add("街道不能为空");
		}

		if (StringUtils.isNotBlank(streetId) && StringUtils.isNotBlank(data.getCommunity())) {
			// 获社区id
			communityId = regionService.getRegionIdByName(data.getCommunity());
			RegionVo regionVo = regionService.queryById(communityId);

			if (regionVo != null && streetId.equals(regionVo.getParentId())) {
				data.setCommunity(communityId);
			} else {
				validateList.add("社区信息编辑不正确");
			}
		} else if (StringUtils.isBlank(data.getCommunity())) {
			validateList.add("社区不能为空");
		}

		if (StringUtils.isBlank(data.getDetailAddress())) {
			validateList.add("地址不能为空");
		}

		if (!StringUtils.isEmpty(data.getMaintenanceTypeName())) {
			String maintenanceType =
					this.dictService.getDictValue(DICT_NAME_MAINTENANCE_TYPE, data.getMaintenanceTypeName());

			if (StringUtils.isNotBlank(maintenanceType)) {
				data.setMaintenanceType(maintenanceType);
			} else {
				validateList.add("管养类型不存在");
			}
		} else {
			validateList.add("管养类型不能为空");
		}

		if (data.getServiceBeginDate() == null || data.getServiceEndDate() == null) {
			validateList.add("服务开始和服务结束时间不能为空");
		}

		if (!validateList.isEmpty()) {
			String errMsg = String.join(" ，", validateList);
			excelResult.getValidateList().add(errMsg);

			data.getDataCheckResult().setSucess(false);
			data.getDataCheckResult().setMsg(errMsg);
		} else {
			data.getDataCheckResult().setSucess(true);
		}
	}

	@Override
	protected void doOnExceptionProcess(String errorMsg) {

	}
}
